
$(function () {

    var productStock_datagrid = $("#productStock_datagrid");
    var reviseNumber_dialog_form = $("#reviseNumber_dialog_form");
    var reviseNumber_dialog = $("#reviseNumber_dialog")
    //库存调拨的dialog
    var alle_dialog = $("#alle_dialog");
    //库存调拨的form
    var alle_form = $("#alle_form");
    var perData = null;

    productStock_datagrid.datagrid({
        url:"productStock/list",
        pagination:true,
        fit:true,
        fitCloumns:true,
        singleSelect:true,
        columns:[[
            {field:"sn",title:"商品编号",width:100},
            {field:"product",title:"商品名称",width:100,formatter:formatterProductName},
            {field:"product",title:"单位",width:100,formatter:formatterUnit},
            {field:"number",title:"库存数量",width:100,formatter:formatterNumber},
            {field:"inputTime",title:"入库时间",width:100},
            {field:"costPrice",title:"单价",width:100},
            {field:"amountPrice",title:"小计",width:100},
            {field:"amountPrice",title:"小计",width:100},
            {field:"supplier",title:"供应商",width:100,formatter:formatterSupplier},
            {field:"lastCheckTime",title:"上次盘点时间",width:100,},
            {field:"operator",title:"操作人员",width:100,formatter:formatterOperator},
            {field:"depot",title:"仓库",width:100,formatter:formatterDepot},
        ]],
        toolbar:"#productStock_dialog",
        onLoadSuccess:function (data) {
            console.log(data);
            perData = $.extend(true,{},data.rows)

        },

     
    })

    cmdObj = {
        //确认数量(盘点)
        productStock_sureNumber:function () {
            var selecedRow = productStock_datagrid.datagrid("getSelected");
            if(!selecedRow){
                $.messager.alert("温馨提醒","请先选中行","error");
                return;
            }
            $.messager.confirm("温馨提醒","您已经确认仓库数据一致了吗？",function (data) {
                console.log(data);
                if (data){
                    $.post("/productStock/audit",{id:selecedRow.id},function (data) {
                        if(data.success){

                            productStock_datagrid.datagrid("reload");
                        }
                    })
                }

            })
        },
        //修改数量
        productStock_reviseNumber:function () {
            var selectedRow = productStock_datagrid.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("温馨提醒","请先选中行","error");
                return;
            }
            console.log(selectedRow);
            //商品
            if(selectedRow.product){
                selectedRow['product.productName'] = selectedRow.product.productName;

            }
            //操作人员
            if(selectedRow.operator){

                selectedRow['operator.id'] = selectedRow.operator.id;
            }
            reviseNumber_dialog_form.form("load",selectedRow)
            reviseNumber_dialog.dialog("open");
        },
        //查询
        productStock_search:function () {
           var keyword = $("#keyword").val();
            productStock_datagrid.datagrid("load",{keyword:keyword})

        },
        //保存修改数量
        reviseNumberSave:function(){

            reviseNumber_dialog_form.form("submit",{
                url:"/productStock/updateReviseNumber",
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("温馨提醒","库存数量修改成功","info",function () {
                            reviseNumber_dialog.dialog("close")
                            productStock_datagrid.datagrid("reload");
                        })
                    }else {
                        $.messager.alert("温馨提醒",data.msg,"error")
                    }
                }
            })

        },
        //库存调拨
        productStock_allocation:function () {
            //清空表单数据
            alle_form.form("clear")
            var selectedRow = productStock_datagrid.datagrid("getSelected");
            if(!selectedRow){
                $.messager.alert("温馨提醒","请先选中行","error");
                return;
            }
            console.log(selectedRow);

            //商品
            if(selectedRow.product){
                selectedRow['product.productName'] = selectedRow.product.productName;

            }
            //所在仓库
            if(selectedRow.depot){
                selectedRow['allDepot.id'] = selectedRow.depot.id;
                //$("#alle_supplier").val(selectedRow.depot.name)

            }
            //供应商
            if(selectedRow.supplier){
                selectedRow['supplier.id'] = selectedRow.supplier.id;

            }
            //加载表单数据
            alle_form.form("load",selectedRow);

            alle_dialog.dialog("open")
        },

        //库存调拨确认
        all_save:function () {
            alle_form.form("submit",{
                url:"/productStock/allocation",
                success:function (data) {
                    console.log(data);
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("温馨提醒","调拨成功","info",function () {
                            productStock_datagrid.datagrid("reload");
                            alle_dialog.dialog("close")
                        })
                    }else{
                        $.messager.alert("温馨提醒",data.msg,"error")
                    }
                }
            })
        },

        cancel:function () {
            reviseNumber_dialog.dialog("close")
            alle_dialog.dialog("close")
        }
    }
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        cmdObj[cmd]();
    })

})

//商品名称
function formatterProductName(value,row,index) {
    return value ? value.productName : "";
}
//单位
function formatterUnit(value,row,index) {
    return value ? value.unit : "";
}
//供应商
function formatterSupplier(value,row,index) {
    return value ? value.name : "";
}
//仓库
function formatterDepot(value,row,index) {
    return value ? value.name : "";
}
//操作人员
function formatterOperator(value,row,index){

    if(value){
        return "<span style='color:blue'>"+value.name+"</span>"
    }

    return "";
}

//数量
function formatterNumber(value,row,index) {
    if(value){
        return "<span style='color: red'>"+value+"</span>"
    }

    return '';

}